/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include <string>
#include <cstdio>
#include <string>
#include <iostream>
#include <fstream>
#include <exception>
#ifdef WIN32
#else
#include <unistd.h>
#endif
#include "BackintTool.h"
#include "BackintConst.h"
#include "log/Log.h"
#include "common/Path.h"
#include "common/File.h"

using namespace backint;

namespace {
const std::string AGENT_CONF = "DataBackup\\ProtectClient\\ProtectClient-E\\conf\\hcpconf.ini";
const std::string BACKINT_LOG_PATH = "DataBackup\\ProtectClient\\ProtectClient-E\\log\\Plugins\\GeneralDBPlugin";
const std::string BACKINT_LOG_NAME = "backint.log";
const uint32_t DEFAULT_LOG_COUNT = 100;
const uint32_t DEFAULT_LOG_SIZE = 100;
}

void InitLog()
{
    std::string agentHome = GetAgentHome();
    agentHome = (agentHome.empty()) ? PARAM_PATH : agentHome;
    std::string confFile = agentHome + AGENT_CONF;

    int iLogLevel = 0;
    int iLogCount = DEFAULT_LOG_COUNT;
    int iLogMaxSize = DEFAULT_LOG_SIZE;
    ParseLogFile(confFile, iLogLevel, iLogCount, iLogMaxSize);

    iLogLevel = (iLogLevel == -1) ? 0 : iLogLevel;
    iLogCount = (iLogCount == -1) ? DEFAULT_LOG_COUNT : iLogCount;
    iLogMaxSize = (iLogMaxSize == -1) ? DEFAULT_LOG_SIZE : iLogMaxSize;

    std::string logPath = agentHome + PATH_SEP_CHAR + BACKINT_LOG_PATH;
    Module::CPath::GetInstance().Init(logPath);
    Module::CLogger::GetInstance().Init(BACKINT_LOG_NAME.c_str(), logPath);
    Module::CLogger::GetInstance().SetLogConf(iLogLevel, iLogCount, iLogMaxSize);
}

void PrintInputParam(int argc, char **argv, const std::string &inputFile)
{
    INFOLOG("Command Parameters:");
    for (int i = 0; i < argc; ++i) {
        INFOLOG("%d:%s", i, argv[i]);
    }
    if (inputFile.empty()) {
        return;
    }
    std::ifstream iniFile(inputFile);
    if (!iniFile.is_open()) {
        ERRLOG("Failed to open file:%s.", inputFile.c_str());
        return;
    }
    DBGLOG("Input backup Files list:");
    std::string line;
    while (std::getline(iniFile, line)) {
        DBGLOG("%s", line.c_str());
    }
    iniFile.close();
}

int main(int argc, char **argv)
{
    setvbuf(stdout, nullptr, _IONBF, 0);
    InitLog();
    std::string inputFile;
    if (GetInFilePath(argc, argv, inputFile) != SUCCESS) {
        ERRLOG("Get in file path failed.");
        return FAILED;
    }
    DBGLOG("The Input File %s:", inputFile.c_str());
    PrintInputParam(argc, argv, inputFile);
    std::string version = "1.5";
    BackintTool *bkt = new BackintTool(version);
    if (bkt->Run(argc, argv) != SUCCESS) {
        ERRLOG("Backint running failed.");
        return FAILED;
    }
    INFOLOG("Backint running success");
    return SUCCESS;
}